package com.yunquan.ohana.ui.calendar.event.week

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.activity.result.ActivityResult
import com.yunquan.ohana.BR
import com.yunquan.ohana.R
import com.yunquan.ohana.base.ui.BaseFragment
import com.yunquan.ohana.databinding.FragmentEventWeekBinding
import com.yunquan.ohana.ui.calendar.event.month.EventMonthFragment
import com.yunquan.ohana.utils.DateUtils
import java.time.LocalDateTime

class EventWeekFragment : BaseFragment<FragmentEventWeekBinding, EventWeekViewModel>() {

    companion object {
        fun newInstance(deviceId: String, userType: Boolean, offset: Int): EventWeekFragment {
            val fragment = EventWeekFragment()
            fragment.arguments = Bundle().apply {
                putInt("offset", offset)
                putString("deviceId", deviceId)
                putBoolean("userType",userType)
            }
            return fragment
        }
    }

    override fun initContentView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): Int {
        return R.layout.fragment_event_week
    }

    override fun initVariableId(): Int {
        return BR.eventWeekViewModel
    }

    override fun initData(savedInstanceState: Bundle?) {
        val bundle = requireArguments()
        val offset = bundle.getInt("offset")
        viewModel!!.mDeviceId = bundle.getString("deviceId")
        viewModel!!.mUserType = bundle.getBoolean("userType")
        val mLocaleDateTime = LocalDateTime.now().plusWeeks(offset.toLong())

        val (start, end) = DateUtils.getWeekRange(mLocaleDateTime)
        viewModel!!.mStartDate = start
        viewModel!!.mEndDate = end
        viewModel!!.initView()
    }

    override fun initViewObservable() {
        super.initViewObservable()
        viewModel!!.ui.observe(viewLifecycleOwner) {
            when (it) {
                0 -> refreshLayout()
            }
        }
    }

    override fun onActivityResult(result: ActivityResult) {
        super.onActivityResult(result)
        if (result.resultCode == 1002) {
           viewModel?.getEventData()
        } else if (result.resultCode == 1003) {
            viewModel?.getEventData()
        }
    }

}